package com.railway.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.railway.bean.Admin;
import com.railway.bean.Contact;
import com.railway.bean.LoseCreditUser;
import com.railway.bean.Page;
import com.railway.bean.Station;
import com.railway.bean.Train;
import com.railway.bean.User;
import com.railway.dao.AdminDao;
import com.railway.dao.ContactDao;
import com.railway.dao.IntegraDao;
import com.railway.dao.LoseCreditDao;
import com.railway.dao.StationDao;
import com.railway.dao.TrainDao;
import com.railway.dao.UserDao;
import com.railway.dao.impl.AdminDaoImpl;
import com.railway.dao.impl.ContactDaoImpl;
import com.railway.dao.impl.IntegraDaoImpl;
import com.railway.dao.impl.LoseCreditDaoImpl;
import com.railway.dao.impl.StationDaoImpl;
import com.railway.dao.impl.TrainDaoImpl;
import com.railway.dao.impl.UserDaoImpl;
import com.railway.service.AdminService;
import com.railway.service.TrainService;

public class AdminServiceImpl implements AdminService{

	ContactDao cd = new ContactDaoImpl();
	UserDao ud = new UserDaoImpl();
	IntegraDao itd = new IntegraDaoImpl();
	LoseCreditDao lcd = new LoseCreditDaoImpl();
	StationDao sd = new StationDaoImpl();
	TrainDao td = new TrainDaoImpl();
	TrainService ts = new TrainServiceImpl();
	AdminDao ad = new AdminDaoImpl();
	
	@Override
	public List<Contact> selectContact(int user_id) {
		return cd.showContactByAdmin(user_id);
	}
	
	@Override
	public Page selectUser(int pageNum, int pageSize) {
		List<User> data = ud.selectUser(pageNum, pageSize);
		for (User user : data) {
			user.setContact(selectContact(user.getUser_id()));
		}
		int totalRows = ud.selectCountUser();
		Page<User> page = new Page(data,pageNum,pageSize,totalRows);
		return page;
	}
	
	@Override
	public int updateUser(Map<String, String[]> map) {
		Set<String> keys = map.keySet();//取出键
		int temp = 0;
		int user_id = 0;
		
		for (String key : keys) {
			if("user_id".equals(key) && "".equals(map.get(key)[0])) {
				user_id = Integer.parseInt(map.get(key)[0]);
			}
		}
		
		for (String key : keys) {
			if("user_password".equals(key) && "".equals(map.get(key)[0])) {
				if(!map.get(key)[0].equals(ud.selectUserPassword(user_id))) {
					temp = ud.updateUserPassWord(user_id, map.get(key)[0]);
				}
			}
			
			if("user_phone".equals(key) && "".equals(map.get(key)[0])) {
				if(!map.get(key)[0].equals(ud.selectUserPhone(user_id))) {
					temp = ud.updateUserPhone(user_id, map.get(key)[0]);
				}
			}
			
			if("user_email".equals(key) && "".equals(map.get(key)[0])){
				if(!map.get(key)[0].equals(ud.selectUserEmail(user_id))) {
					temp = ud.updateUserEmail(user_id, map.get(key)[0]);
				}
			}
			
			if("consumptionPassword".equals(key)&& "".equals(map.get(0)[0])) {
				if(!map.get(key)[0].equals(ud.selectConsumptionPassword(user_id))) {
					temp = ud.updateConsumptionPassword(user_id, map.get(key)[0]);
				}
			}
		}
		
		return temp;
	}

	@Override
	public int deleteUser(int user_id) {
		return ud.deleteUser(user_id);
	}

	@Override
	public int deleteContact(int user_id, int contact_id) {
		return cd.deleteContact(user_id, contact_id);
	}

	@Override
	public int recoverContact(int user_id, int contact_id) {
		return cd.recoverContact(user_id, contact_id);
	}

	@Override
	public int deleteLoseCreditDao(int loseCredit_id) {
		
		return lcd.deleteLoseCreditUser(loseCredit_id);
	}

	@Override
	public List<LoseCreditUser> showLoseCredit(int pageNum, int pageSize) {
		return lcd.selectUserAllByAdmin(pageNum, pageSize);
	}

	@Override
	public int insertLoseCreditUser(String user_name, String user_id_num) {	
		LoseCreditUser lc = new LoseCreditUser(user_name, user_id_num, 1);
		return lcd.insertLoseCredit(lc);
	}

	@Override
	public Page<Station> showStation(int pageNum, int pageSize) {
		List<Station> data = sd.selectStationAll(pageNum, pageSize);
		int totalRows = sd.countStation();
		Page<Station> page = new Page(data,pageNum,pageSize,totalRows);
		return page;
	}

	@Override
	public int insertStation(Station station) {
		return sd.insertStation(station);
	}

	@Override
	public int deleteStation(int station_id) {
		return sd.deleteStation(station_id);
	}

	@Override
	public int updateStation(int station_id, String detailed) {
		Station s = new Station();
		s.setStation_id(station_id);
		s.setStationDetailed(detailed);
		return sd.updateStation(s);
	}

	@Override
	public Page<Train> showTrain(int pageNum, int pageSize) {
		List<Train> data = td.selectTrainAll(pageNum, pageSize);
		for (Train train : data) {
			train.setCamage(td.selectCarriage(train.getTrain_id()));
			train.setPass(td.selectPass(train.getTrain_id()));
		}
		int totalRows = td.selectTrainCount();
		Page<Train> page = new Page<Train>(data, pageNum, pageSize, totalRows);
		return page;
	}

	@Override
	public int deleteTrain(int train_id) {
		Train train = td.selectTranById(train_id).get(train_id);
		Date date = new Date();
		Date date1 = train.getStarting_time();
		Date date2 = train.getTerminal_time();
		
		if(date.getTime()>date1.getTime() && date.getTime()<date2.getTime()) {
			return -1;//列车正在运行,无法删除
		}if(date.getTime()>date2.getTime()) {
			return -2;//列车运行完，无法删除
		}else{
			int temp = td.deleteTrain(train_id);
			td.deletePass(train_id);
			td.deleteCarriage(train_id);
			td.deleteSeat(train_id);
			return temp;
		}
	}

	@Override
	public int insertTrain(Map<String, String[]> map) {
		return ts.addTrain(map);
	}


	@Override
	public int updateAdminPassword(int admin_id, String admin_password) {
		return ad.updateAdmin(admin_id, admin_password);
	}

	@Override
	public Map login(String admin_name, String password) {
		Map map = new HashMap();
		if(ad.selectAdmin(admin_name, password)!= null) {
			map.put("longinAdmin", ad.selectAdmin(admin_name, password));
		}else{
			if(ad.selectAdminByName(admin_name)>0) {
				 map.put("密码错误", "密码错误");
			}else {
				map.put("无此管理员", "无此管理员");
			}
		}
		return map;
	}

	@Override
	public int returnTicketDao(int user_id) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int returnTicketSerive(int trian_id) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int delAbsoluteLcu(int loseCreditId) {
		
		return lcd.delAbsoluteLcu(loseCreditId);
	}

	@Override
	public List<Admin> selectAllAdmin(int pageNum, int pageSize) {
		
		return ad.selectAllAdmin(pageNum, pageSize);
	}

	@Override
	public int selectCountAdmin() {
		
		return ad.selectCountAdmin();
	}

	@Override
	public int deleteAdmin(int adminId) {
		return ad.deleteAdmin(adminId);
	}

	@Override
	public int updateAdminAll(int adminId, String adminName, String adminPassword) {
		
		return ad.updateAdminAll(adminId, adminName, adminPassword);
	}

	@Override
	public int insertAdmin(Admin admin) {
		
		return ad.insertAdmin(admin);
	}

	@Override
	public List<Admin> selectByadminName(String AdminName, int pageNum, int pageSize) {
		
		return ad.selectByadminName(AdminName, pageNum, pageSize);
	}

	@Override
	public int selectCountAdminName(String AdminName) {
		
		return ad.selectCountAdminName(AdminName);
	}
	
	
}
